﻿
using System.Collections.Generic;
using System.Data;
using System;
namespace ZHDJ.MobileInterface
{
    public partial class ExaminTopicSubimt : AbstractBasePage
    {
        protected override OutParam BusinessProcess<InParam, OutParam>(InParam inParam)
        {
            // 业务逻辑返回的响应对象  
            ExaminTopicSubimtOutParam result = new ExaminTopicSubimtOutParam();
            ExaminTopicSubimtInParam param = new ExaminTopicSubimtInParam();
            string logMsg = "";
            bool isFlag = true;
                 
            try
            { 
                bool isAllRight = true;
                string optionList = "";
                string examList = ""; 
                param = inParam as ExaminTopicSubimtInParam;

                if (string.IsNullOrEmpty(param.user_id))
                {
                    result.code = "0";
                    result.desc = "用户名不能为空";

                    isFlag = false;
                }


                if (CommonUtils.IsDisableUsers(param.user_id))
                {
                    result.code = "0";
                    result.desc = "用户状态有问题，请联系管理员";

                    isFlag = false;
                }

                if (isFlag)
                {
                    //用户是否存在
                    isFlag = InterfaceHelper.CheckLoginIDCountEx(param.user_id);

                    if (!isFlag)
                    {
                        result.code = "0";
                        result.desc = "该用户不存在";
                    }
                }  

                if (isFlag)
                {
                    if (param.exam == null || param.exam.Count != 3)
                    {
                        result.code = "0";
                        result.desc = "题目集合,或者选项集合不正确";

                        isFlag = false;
                    }
                    else
                    {
                        foreach (Answer item in param.exam)
                        {
                            if(string.IsNullOrEmpty(item.exam_id) || string.IsNullOrEmpty(item.option_id))
                            {
                                result.code = "0";
                                result.desc = "题目集合,或者选项集合不正确";

                                isFlag = false;
                                break;
                            }
                        }
                    } 
                }

                if (isFlag)
                {
                    string[] options;
                    Answer rightAnswer = new Answer();
                    DataSet dsRightAnswer = new DataSet();
                    int rightNum = 0;
                    for (int i = 0; i < 3; i++)
                    {
                        examList += "'" + param.exam[i].exam_id + "',";
                        options = param.exam[i].option_id.Split(',');
                        foreach (string ii in options)
                            optionList += "'" + ii + "',";

                        if (!InterfaceHelper.TopicOptionIsRight(param.exam[i].exam_id, options))
                        {
                            isAllRight = false;
                        }
                        else
                        {
                            rightNum++;
                        }

                        //获取正确答案
                        dsRightAnswer = InterfaceHelper.GetRightOption(param.exam[i].exam_id);
                        if (dsRightAnswer != null && dsRightAnswer.Tables[0].Rows.Count > 0)
                        {
                            rightAnswer = new Answer();
                            rightAnswer.exam_id = param.exam[i].exam_id;
                            for (int mm = 0; mm < dsRightAnswer.Tables[0].Rows.Count; mm++)
                            {
                                if(mm > 0)
                                    rightAnswer.option_id += "," + dsRightAnswer.Tables[0].Rows[mm]["optionid"].ToString();
                                else
                                    rightAnswer.option_id = dsRightAnswer.Tables[0].Rows[mm]["optionid"].ToString();
                            }
                            result.data.list.Add(rightAnswer);
                        }
                    } 

                    if (optionList.Length > 0)
                        optionList = optionList.Substring(0, optionList.Length - 1);
                    else
                        optionList = "''";
                    examList = examList.Substring(0, examList.Length - 1);

                    string id = StringHelper.NewID;
                    InterfaceHelper.ExaminTopicSubimt(id, isAllRight, param.user_id, optionList, examList);

                    if (isAllRight)
                    {
                        InterfaceHelper.ScoreManagePartyOnlineTestAdd(id, param.user_id, "在线测试积分");
                        result.desc = string.Format("恭喜！您在本次考试中共答对3道题目，将获取{0}个积分(上限为{1}分，超过将不增加积分。)。（以下红色标记是正确答案）", InterfaceHelper.EveryScore, InterfaceHelper.EveryDayScore);
                    }
                    else
                    {
                        result.desc = "您在本次考试中答案不完全正确，因此未能获取积分，请再接再厉！（以下红色标记是正确答案）";
                    }
                }
                 
                logMsg = result.desc;
            }
            catch (Exception ex)
            {
                result.code = "0";
                result.desc = "提交不成功！"; 
                logMsg = ex.Message + ":" + ex.StackTrace;
            }

            CommonUtils.InsertMobileInterfaceLog(SystemCode +"-ExaminTopicSubimt", SerializeHelper.JsonSerializer<ExaminTopicSubimtInParam>(param), logMsg); 
            return result as OutParam;
        }

        protected void Page_Load(object sender, System.EventArgs e)
        {
            if (string.Compare(Request.QueryString["Safe"], "False", true) == 0)
                ProcessRequest<ExaminTopicSubimtInParam, ExaminTopicSubimtOutParam>();
            else
                SafeProcessRequest<ExaminTopicSubimtInParam, ExaminTopicSubimtOutParam>();
        }
    }

    

    
}